package com.kikt.animationdemo.fragment;

import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.kikt.animationdemo.PhotoActivity;
import com.kikt.animationdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by kikt on 2016/1/16.
 */
public class PhotoFragment extends Fragment implements View.OnClickListener {

    @Bind(R.id.iv1)
    ImageView mIv1;
    @Bind(R.id.iv2)
    ImageView mIv2;
    @Bind(R.id.iv3)
    ImageView mIv3;
    private View mRootView;
    protected PhotoActivity activity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_photo, null);
        ButterKnife.bind(this, mRootView);
        init();
        initData();
        return mRootView;
    }

    private void init() {
        activity = (PhotoActivity) getActivity();
    }

    private void initData() {
        mIv1.setOnClickListener(this);
        mIv2.setOnClickListener(this);
        mIv3.setOnClickListener(this);
    }

    protected View findViewById(int viewId) {
        return mRootView.findViewById(viewId);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        FrameLayout.LayoutParams params = null;
        BitmapDrawable drawable = null;
        switch (v.getId()) {
            case R.id.iv1:
                params = (FrameLayout.LayoutParams) mIv1.getLayoutParams();
                drawable = (BitmapDrawable) mIv1.getDrawable();
                break;
            case R.id.iv2:
                params = (FrameLayout.LayoutParams) mIv2.getLayoutParams();
                drawable = (BitmapDrawable) mIv2.getDrawable();
                break;
            case R.id.iv3:
                params = (FrameLayout.LayoutParams) mIv3.getLayoutParams();
                drawable = (BitmapDrawable) mIv3.getDrawable();
                break;

        }
        activity.replaceFragment(new ViewpagerFragment(drawable, params), "h");
    }

    public void test() {
        Toast.makeText(getContext(), "tetet", 0).show();
    }

    private void logRect(Rect outRect) {
        Log.d("PhotoActivity", "outRect.top:" + outRect.top);
        Log.d("PhotoActivity", "outRect.bottom:" + outRect.bottom);
        Log.d("PhotoActivity", "outRect.left:" + outRect.left);
        Log.d("PhotoActivity", "outRect.right:" + outRect.right);
    }
}
